<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button >查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
        :data="currentPageData"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;"
    >
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
        </el-table-column>
        <el-table-column
          prop="userId"
          header-align="center"
          align="center"
          label="用户ID">
        </el-table-column>
        <el-table-column
          prop="username"
          header-align="center"
          align="center"
          label="用户名">
        </el-table-column>
        <el-table-column
          prop="phone"
          header-align="center"
          align="center"
          label="手机号">
        </el-table-column>
        <el-table-column
          prop="email"
          header-align="center"
          align="center"
          label="邮箱">
        </el-table-column>
        <el-table-column
          prop="password"
          header-align="center"
          align="center"
          label="加密密码">
        </el-table-column>
        <el-table-column
          prop="createTime"
          header-align="center"
          align="center"
          label="创建时间">
        </el-table-column>
        <el-table-column
          prop="updateTime"
          header-align="center"
          align="center"
          label="更新时间">
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="gotoDetail(scope.row)">详情/修改</el-button>
            <el-button type="text" size="small" @click="deleteHandle(scope.row.userId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    <div class="page" style="display: inline-block;margin-top: 20px;float:right;">
      <span>共{{dataList.length}}条数据</span>
      <button @click="prevPage()" style=" border: 1px solid #333;padding: 4px;border-radius: 4px;">
        上一页
      </button>
      <span>第{{currentPage}}页/共{{totalPage}}页</span>
      <button @click="nextPage()" style=" border: 1px solid #333;padding: 4px;border-radius: 4px;">
        下一页
      </button>
      前往第<input type="text" id="go" @change="toPage" style="width:20px;text-align: center; border: 1px solid #333;padding: 4px;border-radius: 4px;">页
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './user-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        currentPageData:[],//当前页数据
        totalPage: 1, // 统共页数，默认为1
        currentPage: 1, //当前页数 ，默认为1
        pageSize: 8, // 每页显示数量
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 点击某一行跳转到具体某用户信息的页面
      gotoDetail(row){
        this.$router.replace({
          path:'/verify-userInfo',
          query:{userId:row.userId}
        })
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/generator/user/list'),
          method: 'get',
          params: this.$http.adornParams({
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.page();
          } else {
            this.dataList = []
          }
          this.dataListLoading = false
        })
      },
      //分页
      page(){
        this.totalPage = Math.ceil(this.dataList.length / this.pageSize);
        // 计算得0时设置为1
        this.totalPage = (this.totalPage = 0 ? 1 : this.totalPage);
        this.setCurrentPageData();
      },
      // 设置当前页面数据
      setCurrentPageData() {
        let begin = (this.currentPage - 1) * this.pageSize;
        let end = this.currentPage * this.pageSize;
        this.currentPageData = this.dataList.slice(begin, end);
      },
      //上一页
      prevPage() {
        if (this.currentPage === 1) return;
        this.currentPage--;
        this.setCurrentPageData();
      },
      // 下一页
      nextPage() {
        if (this.currentPage === this.totalPage) return;
        this.currentPage++;
        this.setCurrentPageData();
      },
      //跳转到指定页数
      toPage() {
        this.currentPage= document.getElementById("go").value;
        if(this.currentPage>this.totalPage){
          this.$message.error("超过总页数，请重新输入！");
        }
        this.setCurrentPageData();
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      //多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },

      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.userId
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/generator/user/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>

